package net.caidingke.shepherd.utils.code.image;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author bowen
 */
@Slf4j
public class ImageGen {
    private BufferedImage image;
    private Graphics2D graphicsG2D;
    private static final int DEFAULT_WIDTH = 536;
    private static final int DEFAULT_HEIGHT = 821;

    public ImageGen(int width, int height) {
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        graphicsG2D = image.createGraphics();
        graphicsG2D.setColor(Color.WHITE);
        graphicsG2D.fillRect(0, 0, width, height);
    }

    public static ImageGen create() {
        return new ImageGen(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    public ImageGen image(BufferedImage headImage, int x, int y, int width, int height) {
        Ellipse2D.Double shape = new Ellipse2D.Double(x, y, width, height);
        graphicsG2D.setClip(shape);
        graphicsG2D.drawImage(headImage, x, y, width, height, null);
        graphicsG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        return this;
    }

    public ImageGen nickname(String nickname, int x, int y) {
        graphicsG2D.setFont(graphicsG2D.getFont().deriveFont(25f));
        graphicsG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        graphicsG2D.setColor(Color.BLACK);
        graphicsG2D.drawString(nickname, x, y);
        return this;
    }

    public ImageGen nickname(String nickname, int x, int y, Font font) {
        graphicsG2D.setFont(font);
        graphicsG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        graphicsG2D.setColor(Color.BLACK);
        graphicsG2D.drawString(nickname, x, y);
        return this;
    }

    public ImageGen description(String desc, int x, int y, Font font, Color color) {
        graphicsG2D.setFont(font);
        graphicsG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        graphicsG2D.setColor(color);
        graphicsG2D.drawString(desc, x, y);
        return this;
    }

    public void build() {
        graphicsG2D.dispose();
        image.flush();
        try {
            ImageIO.write(image, "png", new File("page.png"));
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }

    public static void main(String[] args) throws IOException {
//        BufferedImage head = ImageIO.read(new File("/Users/challenge/Downloads/head.png"));
//        BufferedImage qrcode = ImageIO.read(new File("/Users/challenge/Downloads/qrcode.jpg"));
        ImageGen.create()
//                .description("我正在合租趣找室友，快来跟我合租~", 32, 60, new Font("PingFangSC-Regular", Font.PLAIN, 20), new Color(155, 155, 155))
//                .nickname("Pam", 77, 160)
//                .image(head, 32, 134, 36, 36)
//                .image(qrcode, 390, 32, 114, 114)
                .build();

    }
}
